package root.electronclass.electronclass.ext

import android.content.Context
import androidx.core.text.bold
import androidx.core.text.buildSpannedString
import androidx.core.text.scale
import androidx.core.view.isInvisible
import root.electronclass.electronclass.R
import root.electronclass.electronclass.databinding.FragmentClassIndex2ItemBinding
import root.electronclass.electronclass.databinding.FragmentClassIndex4ItemBinding
import kotlin.math.abs

/**
 *
 * @author Lokiy
 * @date 2023/2/4 13:53
 */


fun FragmentClassIndex4ItemBinding.fill(context: Context, rankTypeRes: Int, rank: Int, rankChangeValue: Int) {
    fill(context, rankTypeRes, context.getString(R.string.class_rank, rank), rankChangeValue)
}

fun FragmentClassIndex4ItemBinding.fill(context: Context, rankTypeRes: Int, rank: CharSequence?, rankChangeValue: Int) {
    rank ?: return showEmpty(rankTypeRes)
    classNum.setText(rankTypeRes)
    val rank1 = buildSpannedString {
        append(rank.take(1))
        scale(2.28f) {
            bold {
                append(rank.substring(1, rank.length - 1))
            }
        }
        append(rank.takeLast(1))
    }
    classRank.text = rank1
    rankChange.text = context.getString(if (rankChangeValue >= 0) R.string.class_rank_up else R.string.class_rank_down, abs(rankChangeValue))
    val ic = if (rankChangeValue >= 0) R.drawable.ic_result_up else R.drawable.ic_result_down
    rankChange.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, ic, 0)
    emptyData.isInvisible = true
    rankChange.isInvisible = false
}


fun FragmentClassIndex4ItemBinding.showEmpty(rankTypeRes: Int) {
    classNum.setText(rankTypeRes)
    classRank.text = "-/-"
//    emptyData.isInvisible = false
    classRank.setTextColor(0xFF071129.toInt())
    rankChange.isInvisible = true
}


fun FragmentClassIndex2ItemBinding.fill(context: Context, rankTypeRes: Int, rank: Int, rankChangeValue: Int) {
    classNum.setText(rankTypeRes)
    classRank.text = context.getString(R.string.class_rank, rank)
    classRank.isInvisible = false
    classRank.setTextColor(if (rankChangeValue >= 0) 0xFF15E492.toInt() else 0xFFFF7033.toInt())
    rankChange.text = context.getString(if (rankChangeValue >= 0) R.string.class_rank_up else R.string.class_rank_down, abs(rankChangeValue))
    emptyData.isInvisible = true
    image.isInvisible = false
    rankChange.isInvisible = false
}


fun FragmentClassIndex2ItemBinding.showEmpty(rankTypeRes: Int) {
    classNum.setText(rankTypeRes)
    classRank.text = "-/-"
//    emptyData.isInvisible = false
    classRank.setTextColor(0xFF071129.toInt())
    image.isInvisible = true
    rankChange.isInvisible = true
}
